package cn.tju.yanbo.luckyday;

import java.sql.Date;
import java.util.Calendar;
import java.util.List;
import java.util.Random;

import cn.tju.yanbo.commons.BaseActivity;
import cn.tju.yanbo.commons.CalendarGestureListener;
import cn.tju.yanbo.commons.ImageViewTouchListener;
import cn.tju.yanbo.commons.db.AppDatabase;

import cn.tju.yanbo.R;
import cn.tju.yanbo.calendar.LunarCalendarActivity;
import cn.tju.yanbo.datechoose.DateChooseActivity;
import cn.tju.yanbo.goodoccasion.GoodOccasionActivity;
import cn.tju.yanbo.homepage.HomepageActivity;
import cn.tju.yanbo.setting.SettingActivity;
import cn.tju.yanbo.utils.AppConfig;
import cn.tju.yanbo.utils.AppUtils;

import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.Layout.Alignment;
import android.util.Log;
import android.view.View;
import android.widget.AbsoluteLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

public class LuckyDayActivity extends BaseActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lucky_day);
        
        this.initActivity(savedInstanceState);
        this.setCustomFont(R.id.date_left_day_1, AppUtils.font());
        this.setCustomFont(R.id.date_day, AppUtils.font());
        this.setCustomFont(R.id.date_right_day_1, AppUtils.font());
        this.update(Calendar.getInstance());

        ImageView anchuang = (ImageView)this.findViewById(R.id.yiji_anchuang);
        anchuang.setImageDrawable(CACHED_PICTURES.get("ji_anchuang"));
        ImageView chuxing = (ImageView)this.findViewById(R.id.yiji_chuxing);
        chuxing.setImageDrawable(CACHED_PICTURES.get("ji_chuxing"));
        ImageView dongtu = (ImageView)this.findViewById(R.id.yiji_dongtu);
        dongtu.setImageDrawable(CACHED_PICTURES.get("ji_dongtu"));
        ImageView jisi = (ImageView)this.findViewById(R.id.yiji_jisi);
        jisi.setImageDrawable(CACHED_PICTURES.get("ji_jisi"));
        ImageView nacai = (ImageView)this.findViewById(R.id.yiji_nacai);
        nacai.setImageDrawable(CACHED_PICTURES.get("ji_nacai"));
        
        View view = findViewById(R.id.main_content_view);
        view.setOnTouchListener(new ImageViewTouchListener(this));
    }

    @Override
    protected void registerIntent() {
        registerIntent(R.id.menu_lunar_calendar, LunarCalendarActivity.class);
        registerIntent(R.id.menu_datechoose, DateChooseActivity.class);
        registerIntent(R.id.menu_good_occasion, GoodOccasionActivity.class);
        registerIntent(R.id.menu_setting, SettingActivity.class);
        registerIntent(R.id.menu_homepage, HomepageActivity.class);
    }

    @Override
    protected boolean hasDateView() {
        return true;
    }

    @Override
    protected Class<?> leftActivity() {
        return GoodOccasionActivity.class;
    }

    @Override
    protected Class<?> rightActivity() {
        return SettingActivity.class;
    }
    
    @Override
    protected Drawable background() {
        return CACHED_PICTURES.get(R.drawable.lucky_day);
    }
    
    public int [] getRevolveRadius() {
        return new int[]{AppConfig.DATE_RING_SERIES_RADIUS[0], AppConfig.DATE_RING_SERIES_RADIUS[1]};
    }
    
    public void update(Calendar c) {
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int totalDaysInMonth = AppUtils.getMonthLastDay(year, month);
        
        ImageView view = (ImageView)this.findViewById(R.id.yiji_query);
        String tag = AppUtils.toString(view.getTag());
        int hashCode = Math.abs(tag.hashCode());
        String sc = AppUtils.format("yyyy-MM-dd", c.getTime());
        Date date = Date.valueOf(sc);
        
        int radius = 71;
        Point centroid = new Point(AppUtils.SCREEN_METRICS.widthPixels/2, 170);

        Log.i("gui", "hash_code="+hashCode+",");
        int [] random3Days = AppUtils.randomNDays(date.getTime()+hashCode, 3, totalDaysInMonth);
        this.setViewText(R.id.date_left_day_1, String.valueOf(random3Days[0]));
        this.setViewText(R.id.date_day, String.valueOf(random3Days[1]));
        this.setViewText(R.id.date_right_day_1, String.valueOf(random3Days[2]));
        
        this.setViewPosition(R.id.date_left_day_1, centroid, radius, -120);
        this.setViewPosition(R.id.date_day, centroid, radius, 180);
        this.setViewPosition(R.id.date_right_day_1, centroid, radius, 120);
    }
    
    
    protected void setViewPosition(int id, Point centroid, double radius, double degree) {
        TextView view = (TextView)this.findViewById(id);
        String text = view.getText().toString();
        TextPaint paint = view.getPaint();
        float width = paint.measureText(text); 
        StaticLayout slayout = new StaticLayout(text, paint, (int)width+100 , Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
        int height = slayout.getHeight();

        Log.i("gui", "before:left="+view.getLeft()+",top="+view.getTop()+",right="+view.getRight()+",bottom="+view.getBottom());
        Point position = AppUtils.position(centroid, radius, AppUtils.angle(degree), width, height);
        Log.i("gui", "after:left="+position.x+",top="+position.y+",right="+(position.x+width)+",bottom="+(position.y+height));
        view.setLayoutParams(new LayoutParams((int)width, height, position.x, position.y));
    }
}
